//
//  CouponCon.m
//  takeout
//
//  Created by Mac on 12-12-17.
//
//

#import "CouponCon.h"
#import "Coupon.h"
#import "UICustomImageView.h"
#import "MobClick.h"
#import "Constants.h"

@implementation CouponCon
@synthesize id;
@synthesize imageArray;
@synthesize imageArray2;
@synthesize bgScrView;
@synthesize height;
@synthesize width;


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden=NO;
    
//        //修改导航栏背景
//        NSString *filePath44=[[NSBundle mainBundle] pathForResource:@"searchBarBg-d" ofType:@"png"];
//    
//        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageWithContentsOfFile:filePath44] forBarMetrics: UIBarMetricsDefault];
    
    //添加手势
    UISwipeGestureRecognizer *recognizer;
    recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
    [[self view] addGestureRecognizer:recognizer];
    
    isTouched=YES;
    //定义导航栏左侧按钮
    UIButton *leftButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 54, 27)];
    NSString *filePath2=[[NSBundle mainBundle] pathForResource:@"navLeftBtn" ofType:@"png"];
    [leftButton setBackgroundImage:[UIImage imageWithContentsOfFile:filePath2] forState:UIControlStateNormal];
    [leftButton setTitle:@"返 回" forState:UIControlStateNormal];
    //内容1
    leftButton.titleLabel.font=[UIFont fontWithName:@"Helvetica-Bold" size:14];
    [leftButton addTarget:self action:@selector(backButtonPressd:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithCustomView:leftButton];
    self.navigationItem.leftBarButtonItem = leftItem;
    
    self.imageArray = [self.imageArray arrayByAddingObjectsFromArray:self.imageArray2];
    for(Coupon *coupon in self.imageArray){
        if(self.id==coupon.Id){
            imageUrl=coupon.LargeImgUrl;
            height = coupon.height;
            width = coupon.width;
            break;
        }
    }
    
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    offset = 0.0;
    scale_ = 1.0;
    NSInteger tempHeight=SSiphoneType==0?367:367+IOS5HEIGHT;
    self.imageScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, tempHeight)];
    self.imageScrollView.backgroundColor = [UIColor clearColor];
    self.imageScrollView.scrollEnabled = YES;
    self.imageScrollView.pagingEnabled = YES;
    self.imageScrollView.delegate = self;
    self.imageScrollView.contentSize = CGSizeMake(320, height);
    
    
    UITapGestureRecognizer *doubleTap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTap:)];
    [doubleTap setNumberOfTapsRequired:2];
    
    UIScrollView *s = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, tempHeight)];
    s.backgroundColor = [UIColor clearColor];
    s.contentSize = CGSizeMake(320, tempHeight);
    s.showsHorizontalScrollIndicator = NO;
    s.showsVerticalScrollIndicator = NO;
    s.delegate = self;
    s.minimumZoomScale = 1.0;
    s.maximumZoomScale = 3.0;
    
    [s setZoomScale:1.0];
    
    UICustomImageView *imageview = [[UICustomImageView alloc] init];
//    NSString *imageName = [NSString stringWithFormat:@"%d.jpg",0];
//    imageview.image = [UIImage imageNamed:imageName];
    //        imageview.frame = [self resizeImageSize:imageview.image];
    imageview.frame = CGRectMake(0, 0, 320, tempHeight);
    [imageview setContentMode:UIViewContentModeScaleAspectFit];
    imageview.userInteractionEnabled = YES;
    [imageview setImage:nil];
    [imageview setImageURL:imageUrl];
    
    //        imageview.tag = i+1;
    [imageview addGestureRecognizer:doubleTap];
    [s addSubview:imageview];
    
    [self.imageScrollView addSubview:s];
    

    
        //保存活动id
    [MobClick event:@"couponClicked" label:[[NSString alloc]initWithFormat:@"%d",id]];
    [self.view addSubview:self.imageScrollView];

    

}



//后退手势
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer{
    if(recognizer.direction==UISwipeGestureRecognizerDirectionRight) {
        [self.navigationController popViewControllerAnimated:YES];
        //执行程序
    }
}

- (void)backButtonPressd:(id)sender{
    if(isTouched){
        isTouched=NO;
        //将当前视图推出栈，程序将进入上一视图
        [self.navigationController popViewControllerAnimated:YES];
    }
}



- (void)viewDidUnload
{
    
    imageUrl=nil;
    isTouched=nil;

    self.imageArray2=nil;
    self.imageArray=nil;
    self.bgScrView=nil;
    [super viewDidUnload];
}


-(void)changeCenter:(id)sender{
    
}

#pragma mark - ScrollView delegate
-(UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView{
    
    for (UIView *v in scrollView.subviews){
        return v;
    }
    return nil;
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    
    if (scrollView == self.imageScrollView){
        CGFloat x = scrollView.contentOffset.x;
        if (x==offset){
            
        }
        else {
            offset = x;
            for (UIScrollView *s in scrollView.subviews){
                if ([s isKindOfClass:[UIScrollView class]]){
                    [s setZoomScale:1.0];
                    UIImageView *image = [[s subviews] objectAtIndex:0];
                    image.frame = CGRectMake(20, 0, 320, 411);
                }
            }
        }
    }
}

- (void)scrollViewDidEndZooming:(UIScrollView *)scrollView withView:(UIView *)view atScale:(float)scale {
}

-(void)scrollViewDidZoom:(UIScrollView *)scrollView{
    UIView *v = [scrollView.subviews objectAtIndex:0];
    if ([v isKindOfClass:[UIImageView class]]){
        if (scrollView.zoomScale<1.0){
            //         v.center = CGPointMake(scrollView.frame.size.width/2.0, scrollView.frame.size.height/2.0);
        }
    }
}

#pragma mark -
-(void)handleDoubleTap:(UIGestureRecognizer *)gesture{
    //放大
    float newScale = [(UIScrollView*)gesture.view.superview zoomScale] * 1.5;
    CGRect zoomRect = [self zoomRectForScale:newScale  inView:(UIScrollView*)gesture.view.superview withCenter:[gesture locationInView:gesture.view]];
    UIView *view = gesture.view.superview;
    if ([view isKindOfClass:[UIScrollView class]]){
        UIScrollView *s = (UIScrollView *)view;
        [s zoomToRect:zoomRect animated:YES];
    }
}

#pragma mark - Utility methods

-(CGRect)zoomRectForScale:(float)scale inView:(UIScrollView*)scrollView withCenter:(CGPoint)center {
    
    CGRect zoomRect;
    
    zoomRect.size.height = [scrollView frame].size.height / scale;
    zoomRect.size.width  = [scrollView frame].size.width  / scale;
    
    zoomRect.origin.x    = center.x - (zoomRect.size.width  / 2.0);
    zoomRect.origin.y    = center.y - (zoomRect.size.height / 2.0);
    
    return zoomRect;
}

-(CGRect)resizeImageSize:(CGRect)rect{
    //    NSLog(@"x:%f y:%f width:%f height:%f ", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
    CGRect newRect;
    
    CGSize newSize;
    CGPoint newOri;
    
    CGSize oldSize = rect.size;
    if (oldSize.width>=320.0 || oldSize.height>=411){
        float scale = (oldSize.width/320.0>oldSize.height/411.0?oldSize.width/320.0:oldSize.height/411.0);
        newSize.width = oldSize.width/scale;
        newSize.height = oldSize.height/scale;
    }
    else {
        newSize = oldSize;
    }
    newOri.x = (320.0-newSize.width)/2.0;
    newOri.y = (411.0-newSize.height)/2.0;
    
    newRect.size = newSize;
    newRect.origin = newOri;
    
    return newRect;
}

@end
